//
//  MCMutableAttributedStringViewController.swift
//  MCComponentExtension_Example
//
//  Created by 满聪 on 2019/6/26.
//  Copyright © 2019 CocoaPods. All rights reserved.
//

import UIKit



import MCComponentExtension



class MCMutableAttributedStringViewController: UIViewController {
    // life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
     
        moduleOne()
        
        moduleTwo()
        
        moduleThree()
        
        moduleFour()
        
        moduleFive()
        
        moduleSix()
        
        moduleSeven()
        
        moduleEight()
        
        moduleNine()
        
        moduleTen()
        
        moduleEleven()
        
        moduleTwelve()
        
        moduleThirteen()

        moduleFourteen()
    }
    
    


    
    // MARK: - Setter & Getter
    lazy var scrollView: UIScrollView = {
        let sc = UIScrollView()
        sc.contentSize = CGSize.init(width: 0, height: 1200)
        return sc
    }()
    
    lazy var moduleOneLabel = createLabel()
    lazy var moduleTwoLabel = createLabel()
    lazy var moduleThreeLabel = createLabel()
    lazy var moduleFourLabel = createLabel()
    lazy var moduleFiveLabel = createLabel()
    lazy var moduleSixLabel = createLabel()
    lazy var moduleSevenLabel = createLabel()
    lazy var moduleEightLabel = createLabel()
    lazy var moduleNineLabel = createLabel()
    lazy var moduleTenLabel = createLabel()
    lazy var moduleElevenLabel = createLabel()
    lazy var moduleTwelveLabel = createLabel()
    lazy var moduleThirteenLabel = createLabel()

    lazy var textView: UITextView = {
        let tv = UITextView()
        tv.delegate = self
        tv.isEditable = false
        tv.font = UIFont.systemFont(ofSize: 14)
        tv.text = ""
        tv.linkTextAttributes = [:]
        return tv
    }()
    
}

extension MCMutableAttributedStringViewController {
    
    func moduleOne() {
        
        let text = "字体大小，字体颜色，背景颜色"
        
        scrollView.addSubview(moduleOneLabel)
        moduleOneLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(scrollView)
            make.width.equalTo(UIDevice.width)
            make.top.equalTo(0)
            make.height.equalTo(60)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        // 设置字体大小
        att.mc_addFont(UIFont.systemFont(ofSize: 20), on: NSRange.init(location: 0, length: 4))
        // 设置字体颜色
        att.mc_addForegroundColor(UIColor.red, range: NSRange.init(location: 5, length: 4))
        // 设置背景颜色
        att.mc_addBackgroundColor(UIColor.yellow, range: NSRange.init(location: 10, length: 4))
        moduleOneLabel.attributedText = att
    }
    
    
    func moduleTwo() {
        let text = "字符的间距,字符的间距"
        
        scrollView.addSubview(moduleTwoLabel)
        moduleTwoLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleOneLabel.snp.bottom)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        att.mc_addKern(-2, range: NSRange.init(location: 0, length: 5))
        att.mc_addKern(10, range: NSRange.init(location: 6, length: 5))
        moduleTwoLabel.attributedText = att
    }
    
    func moduleThree() {
        let text = "单细删除线,单粗删除线，双细删除线"
        
        scrollView.addSubview(moduleThreeLabel)
        moduleThreeLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleTwoLabel.snp.bottom)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        att.mc_addStrikethrough(style: .single, color: nil, range: NSRange.init(location: 0, length: 5))
        att.mc_addStrikethrough(style: .thick, color: UIColor.green, range: NSRange.init(location: 6, length: 5))
        att.mc_addStrikethrough(style: .double, color: UIColor.red, range: NSRange.init(location: 12, length: 5))
        
        moduleThreeLabel.attributedText = att
    }
    
    func moduleFour() {
        let text = "单细下划线,单粗下划线，双细下划线"
        
        scrollView.addSubview(moduleFourLabel)
        moduleFourLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleThreeLabel.snp.bottom)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        att.mc_addUnderLine(style: .single, color: nil, range: NSRange.init(location: 0, length: 5))
        att.mc_addUnderLine(style: .thick, color: UIColor.green, range: NSRange.init(location: 6, length: 5))
        att.mc_addUnderLine(style: .double, color: UIColor.purple, range: NSRange.init(location: 12, length: 5))
        
        moduleFourLabel.attributedText = att
    }
    
    func moduleFive() {
        let text = "中空描边 实体描边"
        
        moduleFiveLabel.font = UIFont.systemFont(ofSize: 30)
        scrollView.addSubview(moduleFiveLabel)
        moduleFiveLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleFourLabel.snp.bottom)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        
        // 设置文字颜色无效
        att.mc_addForegroundColor(UIColor.blue, range: NSRange.init(location: 0, length: 4))
        att.mc_addStroke(color: UIColor.red, width: 3, range: NSRange.init(location: 0, length: 4))
        
        att.mc_addForegroundColor(UIColor.orange, range: NSRange.init(location: 5, length: 4))
        att.mc_addStroke(color: UIColor.green, width: -3, range: NSRange.init(location: 5, length: 4))
        
        // 当中空效果下设置ForegroundColor无效
        
        moduleFiveLabel.attributedText = att
    }
    
    func moduleSix() {
        let text = "设置文字阴影"
        
        scrollView.addSubview(moduleSixLabel)
        moduleSixLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleFiveLabel.snp.bottom)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        
        // 设置阴影
        let shadow = NSShadow()
        shadow.shadowColor = UIColor.red
        shadow.shadowOffset = CGSize.init(width: 4, height: 4)
        att.mc_addShadow(shadow, range: NSRange.init(location: 0, length: 6))
        
        moduleSixLabel.attributedText = att
    }
    
    func moduleSeven() {
        let text = "设置文字的特殊效果"
        
        scrollView.addSubview(moduleSevenLabel)
        moduleSevenLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleSixLabel.snp.bottom)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        
        att.mc_addTextEffect(range: NSRange.init(location: 5, length: 4))
        
        moduleSevenLabel.attributedText = att
    }
    
    
    func moduleEight() {
        let text = "偏移量 不要要¥9.99，只要¥0.99"
        
        scrollView.addSubview(moduleEightLabel)
        moduleEightLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleSevenLabel.snp.bottom)
        }
        
        let range1 = NSRange.init(location: 8, length: 1)
        let range2 = NSRange.init(location: 16, length: 1)
        
        let att = NSMutableAttributedString.init(string: text)
        att.mc_addFont(UIFont.systemFont(ofSize: 30), on: range1)
        att.mc_addForegroundColor(UIColor.red, range: range1)
        att.mc_addFont(UIFont.systemFont(ofSize: 30), on: range2)
        att.mc_addForegroundColor(UIColor.green, range: range2)
        
        
        att.mc_addBaselineOffset(-8, range: range2)
        
        
        moduleEightLabel.attributedText = att
    }
    
    func moduleNine() {
        let text = "字体的倾斜度"
        
        scrollView.addSubview(moduleNineLabel)
        moduleNineLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleEightLabel.snp.bottom)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        
        att.mc_addObliqueness(1, range: NSRange.init(location: 0, length: 6))
        moduleNineLabel.attributedText = att
    }
    
    func moduleTen() {
        let text = "横向拉伸"
        
        scrollView.addSubview(moduleTenLabel)
        moduleTenLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleNineLabel.snp.bottom)
        }
        
        let att = NSMutableAttributedString.init(string: text)
        att.mc_addExpansion(1, range: NSRange.init(location: 0, length: 4))
        
        
        moduleTenLabel.attributedText = att
    }
    
    func moduleEleven() {
        let text = "文字书写方向，文字书写方向"
        
        scrollView.addSubview(moduleElevenLabel)
        moduleElevenLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleTenLabel.snp.bottom)
        }
        
        let range1 = NSRange.init(location: 0, length: 6)
        let range2 = NSRange.init(location: 7, length: 6)
        
        let att = NSMutableAttributedString.init(string: text)
 
        att.mc_addWritingDirection(NSWritingDirection.leftToRight, range: range1)
        att.mc_addWritingDirection(NSWritingDirection.rightToLeft, range: range2)

        
        moduleElevenLabel.attributedText = att
    }
    
    func moduleTwelve() {
        let text = "我你，你我么？"
        
        
        
        scrollView.addSubview(moduleTwelveLabel)
        moduleTwelveLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.height.equalTo(moduleOneLabel)
            make.top.equalTo(moduleElevenLabel.snp.bottom)
        }
        
        let image1 = UIImage.init(named: "Player_like_sel")
        let image2 = UIImage.init(named: "Player_like")

        
        
        let att = NSMutableAttributedString.init(string: text)
        
        att.mc_addTextAttachment(image: image1, imageFrame: CGRect.init(x: 0, y: -4, width: 20, height: 20), atIndex: 1)
        att.mc_addTextAttachment(image: image2, imageFrame: CGRect.init(x: 0, y: -4, width: 20, height: 20), atIndex: 6)

        
        moduleTwelveLabel.attributedText = att

    }
    
    func moduleThirteen() {
        let text = "设置行间距\n设置行间距\n设置行间距"
        
        
        moduleThirteenLabel.backgroundColor = UIColor.lightGray
        scrollView.addSubview(moduleThirteenLabel)
        moduleThirteenLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(moduleOneLabel)
            make.top.equalTo(moduleTwelveLabel.snp.bottom)
            make.height.equalTo(160)
        }
        
        
        let att = NSMutableAttributedString.init(string: text)
        
        let style = NSMutableParagraphStyle.init()
        style.lineSpacing = 20
        style.alignment = .center
        
        
        att.mc_addLineSpacing(style, range: NSRange.init(location: 0, length: text.count))
        
        moduleThirteenLabel.attributedText = att

    }
    
    func moduleFourteen() {
        scrollView.addSubview(textView)
        textView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(moduleOneLabel)
            make.top.equalTo(moduleThirteenLabel.snp.bottom)
            make.height.equalTo(80)
        }
 
        textView.appendLinkString("你想知道")
        textView.appendLinkString("富文本是什么？", withURLString: "http://www.baid.com", isShowUnderline: true, underlineColor: UIColor.red)
        
        let att = NSMutableAttributedString.init(attributedString: textView.attributedText)
        att.mc_addForegroundColor(UIColor.red, range: NSRange.init(location: 4, length: 5))
        textView.attributedText = att

    }
}


//MARK: UI的处理,通知的接收
extension MCMutableAttributedStringViewController {
    
    func baseSetting() {
        self.title = "富文本的使用"
        view.backgroundColor = UIColor.white
    }
    
    func initUI() {
        view.addSubview(scrollView)
        scrollView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
    }
    
    func createLabel() -> UILabel {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.black
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }
}

//MARK: 代理方法
extension MCMutableAttributedStringViewController: UITextViewDelegate {
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        
        if let scheme = URL.scheme {
            print(scheme)
            
            switch scheme {
            case "http":
                print("http~~~")
//                return false
            default:
                print("default")
            }
        }
        return true
    }
    
}
